import { ServiceException } from './../../common/excpetion/serviceException';
import { PlaceUseLog } from './../../domain/models/placeUseLog';
import * as Koa from 'koa';
import { route, RouteOptionsVerify, ServiceManager, UsingLock } from "../../common";
import { PlaceService } from '../../service/placeService';
const pSvr = ServiceManager.Current.GetService<PlaceService>('PlaceService');
class PlaceController {
    @route(new RouteOptionsVerify('GET', '/api/place'))
    async queryPlaces(ctx: Koa.Context, next) {
        let queryParams = ctx.query;
        ctx.response.body = await pSvr.QueryPlace(queryParams);
        await next();
    }
    @route(new RouteOptionsVerify('GET', '/api/place/:id'))
    async getPlaceInfoById(ctx: Koa.Context, next) {
        let { id } = ctx.params;
        ctx.response.body = await pSvr.GetPlaceById(id);
        await next();
    }
    @route(new RouteOptionsVerify('POST', '/api/place'))
    async createPlace(ctx: Koa.Context, next) {
        let place = ctx.request.body;
        await UsingLock(place.id, async () => {
            return await pSvr.CreatePlace(place);
        });
        ctx.response.body = place;
        await next();
    }
    @route(new RouteOptionsVerify('PUT', '/api/place'))
    async updatePlace(ctx: Koa.Context, next) {
        let place = ctx.request.body;
        await UsingLock(place.id, async () => {
            return await pSvr.UpdatePlace(place);
        });
        ctx.response.body = place;
        await next();
    }
    @route(new RouteOptionsVerify('DELETE', '/api/place/:id'))
    async deletePlace(ctx: Koa.Context, next) {
        let { id } = ctx.params;
        await UsingLock(id, async () => {
            return await pSvr.DeletePlaceById(id);
        });
        ctx.response.body = {};
        await next();
    }

    @route(new RouteOptionsVerify('GET', '/api/placeType'))
    async queryPlaceTypes(ctx: Koa.Context, next) {
        let queryParam = ctx.query;
        ctx.response.body = await pSvr.QueryPlaceType(queryParam);
        await next();
    }
    @route(new RouteOptionsVerify('GET', '/api/placeType/:id'))
    async getPlaceTypeInfoById(ctx: Koa.Context, next) {
        ctx.response.body = null;
        await next();
    }
    @route(new RouteOptionsVerify('POST', '/api/placeType'))
    async createPlaceType(ctx: Koa.Context, next) {
        let placeType = ctx.request.body;
        await UsingLock(placeType.id, async () => {
            return await pSvr.CreatePlaceType(placeType);
        });
        ctx.response.body = placeType;
        await next();
    }
    @route(new RouteOptionsVerify('PUT', '/api/placeType'))
    async updatePlaceType(ctx: Koa.Context, next) {
        let placeType = ctx.request.body;
        await UsingLock(placeType.id, async () => {
            return await pSvr.UpdatePlaceType(placeType);
        });
        ctx.response.body = placeType;
        await next();
    }
    @route(new RouteOptionsVerify('DELETE', '/api/placeType/:id'))
    async deletePlaceType(ctx: Koa.Context, next) {
        let { id } = ctx.params;
        await UsingLock(id, async () => {
            return await pSvr.DeletePlaceTypeById(id);
        });
        ctx.response.body = {};
        await next();
    }


    @route(new RouteOptionsVerify('POST', '/api/placeUseLog'))
    async createPlaceUseLog(ctx: Koa.Context, next) {
        let useLog: PlaceUseLog = ctx.request.body;
        await UsingLock(useLog.placeId, async () => {
            //检查是否有占用记录
            const useLogList = await pSvr.QueryPlaceUseLogs({
                placeId: useLog.placeId
            });

            if (useLogList.some(x => (x.beginTime <= useLog.beginTime && x.endTime >= useLog.beginTime) || (x.beginTime <= useLog.endTime && x.endTime >= useLog.endTime))) {
                throw new ServiceException('该时间段已被占用！');
            }

            return await pSvr.CreatePlaceUseLog(useLog);
        });
        ctx.response.body = useLog;
        await next();
    }

    @route(new RouteOptionsVerify('DELETE', '/api/placeUseLog/:id'))
    async deletePlaceUseLog(ctx: Koa.Context, next) {
        let { id } = ctx.params;
        await UsingLock(id, async () => {
            return await pSvr.DeletePlaceUseLog(id);
        });
        ctx.response.body = {};
        await next();
    }

    @route(new RouteOptionsVerify('GET', '/api/placeUseLog'))
    async queryPlaceUseLog(ctx: Koa.Context, next) {
        let params = ctx.query;
        ctx.response.body = await pSvr.QueryPlaceUseLogs(params);
        await next();
    }

}
